package com.shuma.shopmanage.draftmodule.controller;

import com.shuma.shopmanage.common.constant.RoleConstant;
import com.shuma.shopmanage.common.exception.ShopManageException;
import com.shuma.shopmanage.common.model.LoginUser;
import com.shuma.shopmanage.common.result.Result;
import com.shuma.shopmanage.common.result.ResultCodeEnum;
import com.shuma.shopmanage.model.draft.model.GoodsDraft;
import com.shuma.shopmanage.model.draft.model.vo.GoodsDraftVo;
import com.shuma.shopmanage.model.shop.dto.GoodsDto;
import com.shuma.shopmanage.draftmodule.service.GoodsDraftService;
import com.shuma.shopmanage.model.shop.dto.PageDto;
import com.shuma.shopmanage.model.shop.vo.GoodsVo;
import com.shuma.shopmanage.model.shop.vo.PageVo;
import com.shuma.shopmanage.security.SecurityContextHolder;
import com.shuma.shopmanage.security.annotation.RequireRole;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "草稿模块")
@RestController
@RequestMapping("/draft")
public class GoodsDraftController {
    @Autowired
    private GoodsDraftService goodsDraftService;
    @PostMapping("/add")
    @RequireRole({RoleConstant.USER})
    public Result addGoodsDraft(@RequestBody GoodsDto goodsDraft){

        //管理员不能执行此操作
        try {
            GoodsVo goodsVo = goodsDraftService.insertGoodDraft(goodsDraft);
            return Result.ok(goodsVo);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ShopManageException(ResultCodeEnum.DATA_ERROR);
        }

    }

    @ApiOperation("分页查询")
    @GetMapping("/page")
    public Result<PageVo<GoodsDraftVo>> queryPage(PageDto pageDto){

        LoginUser loginUser = SecurityContextHolder.getLoginUser();
        PageVo pageVo;
        if (RoleConstant.ADMIN.equals(loginUser.getRole())) {
            pageVo = goodsDraftService.queryPage(pageDto);
        } else {
            Long id = loginUser.getId();
            pageVo = goodsDraftService.queryPageByUid(pageDto, id);
        }
        return Result.ok(pageVo);
    }

    @ApiOperation("删除草稿")
    @RequireRole(RoleConstant.USER)
    @DeleteMapping("/{id}")
    public Result deleteById(@ApiParam("草稿ID") @PathVariable("id") Long id){
        Long userId = SecurityContextHolder.getLoginUser().getId();
        goodsDraftService.deleteByIdWithUid(id, userId);
        return Result.ok();
    }

    @ApiOperation("批量删除草稿")
    @RequireRole(RoleConstant.USER)
    @DeleteMapping
    public Result deleteByIds(@ApiParam(value = "草稿ID数组", required = true)@RequestBody List<Long> ids){
        Long userId = SecurityContextHolder.getLoginUser().getId();
        goodsDraftService.deleteByIdsWithUid(ids, userId);
        return Result.ok();
    }

    @ApiOperation("草稿编辑")
    @PostMapping("/goodsEditing")
    @RequireRole(RoleConstant.USER)
    public Result<GoodsDraft> goodsEditing(Long draftId){
        Long userId = SecurityContextHolder.getLoginUser().getId();
        GoodsDraft goodsDraft = goodsDraftService.goodsEditing(draftId,userId);
        return Result.ok(goodsDraft);
    }
}
